package com.han.http.util;

import com.han.http.client.exception.NullContentMatchedException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @fileName: Regex
 * @author: Han
 * @date: 2018/6/4 10:26
 * @dscription: 正则表达式解析类
 */
public class Regex {

    /**
     * 使用指定正则表达式查找目的字符串
     *
     * @param originalStr 原始字符串
     * @param regex       正则表达式
     * @return 目的字符串
     */
    public static String matchString(String originalStr, String regex) throws NullContentMatchedException {
        String desc;
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(originalStr);
        if (matcher.find()) {
            desc = matcher.group();
        } else {
            //如果没有匹配的值,抛出异常
            throw new NullContentMatchedException();
        }

        return desc;
    }
}